package bo;

import dao.AcaoDAO;
import java.util.ArrayList;
import java.util.List;
import model.Acao;
import util.CustomException;

public class AcaoBO extends AbstractBO {

    private AcaoDAO acaoDAO = new AcaoDAO();

    public List<Acao> selecionarTodos() throws CustomException {
        List<Acao> acaoList = new ArrayList();
        try {
            beginTransaction();
            acaoList = acaoDAO.selecionarListaPorCriterio(getSession());
            commitTransaction();
        } catch (CustomException e) {
            rollbackTransaction();
            throw e;
        }
        return acaoList;
    }

    public Acao selecionarPorId(Long usuCodigo) throws CustomException {
        Acao acao = null;
        try {
            beginTransaction();
            acao = acaoDAO.selecionarPorId(getSession(), usuCodigo);
            commitTransaction();
        } catch (CustomException e) {
            rollbackTransaction();
            throw e;
        }
        return acao;
    }

    public void salvar(Acao acao) throws CustomException {
        try {
            beginTransaction();
            if (acao.getAcaoCodigo() == 0) {
                acao.setAcaoCodigo(acaoDAO.selecionarProximoId(getSession()));
            }
            acaoDAO.salvar(getSession(), acao);
            commitTransaction();
        } catch (CustomException e) {
            rollbackTransaction();
            throw e;
        }
    }

    public void excluir(Acao acao) throws CustomException {
        try {
            beginTransaction();
            acaoDAO.excluir(getSession(), acao);
            commitTransaction();
        } catch (CustomException e) {
            rollbackTransaction();
            throw e;
        }
    }
}
